﻿///////////////////////////////////////////////////////////////////////////////
// File:        Worker.hpp
// Author:      671643387@qq.com
// Description:
///////////////////////////////////////////////////////////////////////////////


#ifndef PINGPONG_WORKER_HPP_
#define PINGPONG_WORKER_HPP_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <boost/unordered_map.hpp>
#include <nio/base/Thread.hpp>
#include "Client.hpp"

class Worker : public nio::base::Thread
{
public:
    Worker(void);
    virtual ~Worker(void);

    bool open(std::string addr, int port, unsigned int bufSize, int clients, std::string message);

    virtual void run(void);
    virtual void stop(void);

private:
    void closeClient(int id);
    void doCloseClient(int id);
    void doStop(void);

    void doClear(void);

private:
    typedef boost::unordered_map<int, ClientPtr> ClientMap;

private:
    int clientIdGen_;

    nio::net::EventLoop loop_;

    ClientMap clients_;
};

typedef boost::shared_ptr<Worker> WorkerPtr;

#endif //PINGPONG_WORKER_HPP_
